package tc.de.krawallmann.gravoblocks.game.model;

import java.util.ArrayList;
import java.util.List;

/**
 * The Class Grid.
 * 
 * @param <T>
 *            the generic type
 * @author KrawallMann
 * @version 1.0
 */
public class Grid<T extends Block> {

	/** The blocks. */
	private List<List<T>> blocks;

	/** The height. */
	private int height;

	/** The width. */
	private int width;

	/**
	 * Instantiates a new grid.
	 * 
	 * @deprecated For deserialization only!
	 */
	@Deprecated
	public Grid() {
		super();
		blocks = new ArrayList<List<T>>();
	}

	/**
	 * Instantiates a new grid.
	 * 
	 * @param width
	 *            the width
	 * @param height
	 *            the height
	 */
	public Grid(final int width, final int height) {
		super();
		this.width = width;
		this.height = height;
		blocks = new ArrayList<List<T>>();
		for (int i = 0; i < height; i++) {
			blocks.add(new ArrayList<T>());
		}
	}

	/**
	 * Clears this grid.
	 */
	public void clear() {
		blocks.clear();
		for (int i = 0; i < height; i++) {
			blocks.add(new ArrayList<T>());
		}
	}

	/**
	 * Gets the block at (x|y).
	 * 
	 * @param xPosition
	 *            the x position
	 * @param yPosition
	 *            the y position
	 * @return the block at (x|y)
	 */
	public T getBlockAt(final int xPosition, final int yPosition) {
		return blocks.get(yPosition).get(xPosition);
	}

	/**
	 * Gets the blocks grid.
	 * 
	 * @return the blocks grid
	 */
	public List<List<T>> getBlocks() {
		return blocks;
	}

	/**
	 * Gets the height.
	 * 
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * Gets the row.
	 * 
	 * @param yPosition
	 *            the y position
	 * @return the row
	 */
	public List<T> getRow(final int yPosition) {
		return blocks.get(yPosition);
	}

	/**
	 * Gets the width.
	 * 
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Sets the block at (x|y).
	 * 
	 * @param xPosition
	 *            the x position
	 * @param yPosition
	 *            the y position
	 * @param block
	 *            the block to set
	 */
	public void setBlockAt(final int xPosition, final int yPosition, final T block) {
		blocks.get(yPosition).set(xPosition, block);
	}

	/**
	 * Sets the blocks grid.
	 * 
	 * @param objects
	 *            the new objects
	 * @deprecated For deserialization only!
	 */
	@Deprecated
	public void setBlocks(final List<List<T>> objects) {
		this.blocks = objects;
	}

	/**
	 * Sets the height.
	 * 
	 * @param height
	 *            the new height
	 * @deprecated For deserialization only!
	 */
	@Deprecated
	public void setHeight(final int height) {
		this.height = height;
		blocks.clear();
		for (int i = 0; i < height; i++) {
			blocks.add(new ArrayList<T>());
		}
	}

	/**
	 * Sets the width.
	 * 
	 * @param width
	 *            the new width
	 * @deprecated For deserialization only!
	 */
	@Deprecated
	public void setWidth(final int width) {
		this.width = width;
	}
}
